package brcl.config;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拦截器
 *
 * @version v1.0
 * @className: InterceptorConfig
 * @author: heyiheng
 * @date: 2020-11-28 09:44
 */
public class InterceptorConfig implements HandlerInterceptor {
	@Value("${url.path}")
	private Boolean path;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		String url = request.getServletPath();
		if (path){
			System.out.println("请求路径："+url);
			System.out.println(JSON.toJSONString(request.getParameterMap()));
		}
		response.setHeader("Access-Control-Allow-Origin", request.getHeader("origin"));
		response.setHeader("Access-Control-Allow-Credentials", "true");
		response.setHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS");
		response.setHeader("Access-Control-Max-Age", "86400");
		response.setHeader("Access-Control-Allow-Headers", "x-requested-with,content-type,accessToken");
		// 如果是OPTIONS请求则结束
		if (HttpMethod.OPTIONS.toString().equals(request.getMethod())) {
			response.setStatus(HttpServletResponse.SC_ACCEPTED);
			return false;
		}
		//拦截器，后续登录判断加这里
		return true;
	}
}
